<?php
//创建基于百雄的测试数据
namespace app\api\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\common\controller\ksapi\Ksapi;
//use tool\Snowflake;
use think\Log;

class CreateTestData extends Command
{
    //查询类目信息列表
    protected $categoryApi = 'open.distribution.public.category.list';

    protected $ksApi;

    //protected $snowflake;


    protected function configure()
    {
        $this->setName('create_test_data');
    }



    /**
     * 递归存储数据到数据库
     * @param array $data 待处理的数据
     * @param int $categoryPid 父级ID
     * @param int $hierarchy 层级数
     * @return void
     */
    public function insertCategory($data, $categoryPid = 0, $hierarchy = 1)
    {

        foreach ($data as $key => $item) {
            // 插入当前分类
            db('category')->insert([
                'categoryId' => $item['categoryId'],
                'categoryName' => $item['categoryName'],
                'categoryPid' => $item['categoryPid'],
                'sortNum' => $item['sortNum'],
                'hierarchy' => $item['hierarchy'],
                'disabled' => isset($item['disabled']) ? $item['disabled'] : 0,
                'disabledMsg' =>  isset($item['disabledMsg']) ? $item['disabledMsg'] : 0,
            ]);
            // 如果有子分类，则递归插入子分类
            if (!empty($item['childCategory'])) {
                $this->insertCategory($item['childCategory'], $item['categoryId'], $item['hierarchy'] + 1);
            }
        }
        return true;
    }



    //完成一半的线上测试数据创建（还差更改itemId和activityId等），这个方案还未想完善，暂时搁置
    protected function execute(Input $input, Output $output)
    {
        /*******************************递归同步栏目分类 临时使用start******************************/
        $categoryData = (new Ksapi())->Api($this->categoryApi,[],$method = 'get',1,1);
        /*********所有类目***********/
        if (isset($categoryData['result']) && $categoryData['result'] == 1){

            $rrr = $this->insertCategory($categoryData['data']);
            var_dump($rrr);die;

        }












































        die;
        /*******************************临时使用end******************************/

        $sql = "select a.id from ks_z_api_activity_goods as a left join ks_z_claim as b on a.id = b.activity_goods_id where a.t_id = 1 and a.claimStatus = 2 and b.id is null";
        $rrr = db()->query($sql);
        foreach($rrr as $kk => $vv){
            db('z_api_activity_goods')->where('id',$vv['id'])->update(['claimStatus'=>1]);
        }


        exit('wancheng');
        //创建技术测试团长活动数据
        $activity = db('z_api_activity')->where('t_id',1)->select();
        foreach($activity AS $kkk => $vvv){
            $activityId = $vvv['activityId'];//原始ID
            $vvv['activityId'] = $kkk+1;
            $vvv['t_id'] = 3;
            $testId = db('z_api_activity')->insertGetId($vvv);
            $goods = db('z_api_activity_goods')->where('activityId',$activityId)->select();
            foreach($goods as $key => $value){
                $activityGoodId = $value['id'];//原始商品ID
                $value['id'] = $key+1;
                $value['t_id'] = 3;
                $value['activityId'] = $testId;
                $testGoodId = db('z_api_activity_goods')->insertGetId($value);

                $goodsContact = db('z_api_activity_goods_contact')->where('activity_goods_id',$activityGoodId)->select();
                foreach ($goodsContact as $kkkk => $vvvv) {
                    $vvvv['activity_goods_id'] = $testGoodId;
                    $vvvv['t_id'] = 3;
                    unset($vvvv['id']);
                    db('z_api_activity_goods_contact')->insertGetId($vvvv);
                }

                $goodsOperator = db('z_api_activity_goods_operator')->where('activity_goods_id',$activityGoodId)->select();
                foreach ($goodsOperator as $kkkkk => $vvvvv) {
                    $vvvvv['activity_goods_id'] = $testGoodId;
                    $vvvvv['t_id'] = 3;
                    unset($vvvvv['id']);
                    db('z_api_activity_goods_operator')->insertGetId($vvvvv);
                }

                $goodsDetail = db('ks_z_api_goods_detail')->where('activity_goods_id',$activityGoodId)->select();
                foreach ($goodsDetail as $kkkkkk => $vvvvvv) {
                    $detailId = $vvvvvv['id'];//原始detailId
                    $vvvvvv['activity_goods_id'] = $testGoodId;
                    $vvvvvv['t_id'] = 3;
                    unset($vvvvvv['id']);
                    $testGoodsDetailId = db('ks_z_api_goods_detail')->insertGetId($vvvvvv);//测试detailId
                    $detailImage = db('z_api_goods_detail_image')->where('goods_detail_id',$detailId)->select();
                    foreach($detailImage as $imgKey => $imgValue){
                        $imgValue['t_id'] = 3;
                        $imgValue['activity_goods_id'] = $testGoodId;
                        $imgValue['goods_detail_id'] = $testGoodsDetailId;
                        unset($imgValue['id']);
                        db('z_api_goods_detail_image')->insertGetId($imgValue);//测试detailId
                    }


                    $lunbo = db('z_api_goods_lunbo')->where('goods_detail_id',$detailId)->select();
                    foreach($lunbo as $lunboKey => $lunboValue){
                        $lunboValue['t_id'] = 3;
                        $lunboValue['activity_goods_id'] = $testGoodId;
                        $lunboValue['goods_detail_id'] = $testGoodsDetailId;
                        unset($lunboValue['id']);
                        db('z_api_goods_lunbo')->insertGetId($lunboValue);//测试detailId
                    }

                    $sku = db('z_api_goods_sku')->where('goods_detail_id',$detailId)->select();
                    foreach($sku as $skuKey => $skuValue){
                        $skuValue['t_id'] = 3;
                        $skuValue['activity_goods_id'] = $testGoodId;
                        $skuValue['goods_detail_id'] = $testGoodsDetailId;
                        unset($skuValue['id']);
                        db('z_api_goods_sku')->insertGetId($skuValue);//测试detailId
                    }
                }
            }
        }
        exit('活动测试数据创建完成');
    }
}
